package com.elephant.bean.selftag;

import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.util.StringUtils;
import org.w3c.dom.Element;

/**
 * @author 林
 * @create 2025-05-16 17:44
 */
//解析XMl的类,参照PropertyPlaceholderBeanDefinitionParser
public class UserBeanDefinitionParser extends AbstractSingleBeanDefinitionParser {

	private static final String SYSTEM_PROPERTIES_MODE_ATTRIBUTE = "system-properties-mode";

	private static final String SYSTEM_PROPERTIES_MODE_DEFAULT = "ENVIRONMENT";


	@Override
	@SuppressWarnings("deprecation")
	//返回的类型
	protected Class<?> getBeanClass(Element element) {

		return User.class;
	}

	@Override
	protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
		// 解析 XSD 中定义的所有属性
		String id = element.getAttribute("id");
		String username = element.getAttribute("username");
		String email = element.getAttribute("email");
		String password = element.getAttribute("password");

		if (element.hasAttribute("id")) {
			builder.addPropertyValue("id", element.getAttribute(id));
		}
		if (element.hasAttribute("username")) {
			builder.addPropertyValue("username", element.getAttribute(username));
		}
		if (element.hasAttribute("email")) {
			builder.addPropertyValue("email", element.getAttribute(email));
		}
		if (element.hasAttribute("password")) {
			builder.addPropertyValue("password", element.getAttribute(password));
		}
	}

}
